package com.fesine.learn.controller;

import com.fesine.commons.util.CryptographyUtil;
import com.fesine.learn.freemarker.SortMethod;
import com.fesine.learn.po.UserInfoPo;
import com.fesine.learn.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 类描述
 * @author: Fesine
 * @createTime:2017/5/8 14:42
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2017/5/8 14:42
 */
@Controller
@RequestMapping("/")
public class FreemarkerController {
    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/list")
    public String list(HttpServletRequest req, ModelMap map)
            throws Exception {
        UserInfoPo u = createUserInfo(req);
        String offset = req.getParameter("start");
        String pageSize = req.getParameter("limit");
        if (!StringUtils.isEmpty(offset) && !StringUtils.isEmpty(pageSize)) {
            u.setOffset(Integer.parseInt(offset));
            u.setPageSize(Integer.parseInt(pageSize));
        }
        map.put("success", true);
        map.put("msg", "<font color='red'>查询用户信息成功!</font>");
        Map<String, String> mapTemp = new HashMap<>();
        mapTemp.put("fesine","好员工");
        mapTemp.put("soly","好员工1");
        mapTemp.put("pactera","好公司");
        map.put("map", mapTemp);
        try {
            List<UserInfoPo> list = userInfoService.listUserInfo(u);
            int total = userInfoService.getTotal(u);
            map.put("userInfoList", list);
            map.put("total", total);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", "查询用户信息失败!");
        }
        return "index";
    }

    @RequestMapping("/role")
    public String role(HttpServletRequest req, ModelMap map)
            throws Exception {
        return "role";
    }

    @RequestMapping("/marco")
    public String marco(HttpServletRequest req, ModelMap map)
            throws Exception {
        return "marco";
    }

    @RequestMapping("/sort")
    public String sort(HttpServletRequest req, ModelMap map)
            throws Exception {
        //将自定义函数返回到前端，供freemarker调用
        map.addAttribute("sort_int", new SortMethod());
        return "sort";
    }

    @RequestMapping("/index")
    public ModelAndView index(HttpServletRequest req)
            throws Exception {
        ModelAndView mv = new ModelAndView();
        UserInfoPo u = createUserInfo(req);
        String offset = req.getParameter("start");
        String pageSize = req.getParameter("limit");
        if (!StringUtils.isEmpty(offset) && !StringUtils.isEmpty(pageSize)) {
            u.setOffset(Integer.parseInt(offset));
            u.setPageSize(Integer.parseInt(pageSize));
        }
        mv.addObject("success", true);
        mv.addObject("msg", "查询用户信息成功!");
        try {
            List<UserInfoPo> list = userInfoService.listUserInfo(u);
            int total = userInfoService.getTotal(u);
            mv.addObject("userInfoList", list);
            mv.addObject("total", total);
        } catch (Exception e) {
            mv.addObject("success", false);
            mv.addObject("msg", "查询用户信息失败!");
        }
        return mv;
    }

    /**
     * 将前端请求字段转换为UserInfoPo对象
     *
     * @param req
     * @return
     */
    private UserInfoPo createUserInfo(HttpServletRequest req) {
        UserInfoPo u = new UserInfoPo();
        u.setId(req.getParameter("id"));
        u.setUserName(req.getParameter("userName"));
        if (!StringUtils.isEmpty(req.getParameter("password")))
            u.setPassword(CryptographyUtil.md5(req.getParameter("password"), "fesine"));
        u.setSex(req.getParameter("sex"));
        u.setNickName(req.getParameter("nickName"));
        u.setCellPhone(req.getParameter("cellPhone"));
        u.setAddress(req.getParameter("address"));
        u.setHeadPhoto(req.getParameter("headPhoto"));
        u.setInfoDesc(req.getParameter("infoDesc"));
        return u;
    }


}
